#include "Missiles.h"
#include <math.h>

// Material Properties
GLfloat missile_diffuse[] = {0.7, 0.7, 0.7, 1.0};
GLfloat missile_ambient[] = {0.7, 0.7, 0.7, 1.0};
GLfloat missile_specular[] = {0.5, 0.5, 0.5, 1.0};

// Shininess
GLfloat missile_shininess = 10.0f;

Missiles::Missiles(void)
{
	width = 1;		// Z
	lenght = 1;		// X
	height = 0.1;	// Y
	sizeMissiles = 0.08;
	lenghtMissiles = 1.5;
	angle = 0;
	oldX = 0;
	oldZ = 0;
	quad = gluNewQuadric();
	loadImage();
}

void Missiles::loadImage()
{
	Image* image1 = loadBMP("textures\\textureBipod.bmp");
	Image* image2 = loadBMP("textures\\textureBipod2.bmp");
	textureBase1 = loadTexture(image1);
	textureBase2 = loadTexture(image2);

	currentTextureBase = textureBase1;

	delete image1;
	delete image2;
}

void Missiles::switchTexture()
{
	if(currentTextureBase == textureBase1)
		currentTextureBase = textureBase2;
	else
		currentTextureBase = textureBase1;
}

void Missiles::setAngle(GLfloat a)
{
	angle = a;
}

void Missiles::display()
{
	GLfloat norm;
	glPushMatrix();
		glTranslatef(0.0, elevation, 0.0);

		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, missile_diffuse);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, missile_ambient );
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, missile_specular);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, missile_shininess);

		glColor3f(1,1,1);
		glTranslatef(0.0, height/2, 0.0);
		
		// BASE
		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D, currentTextureBase);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			//glColor3f(1.0f, 1.0f, 1.0f);

			glPushMatrix();
				glScalef(lenght, height, width);
				drawCube(0.5,0.55);
			glPopMatrix();
			
			glPushMatrix();
				glTranslatef(lenght/2, height/2, -width/2);
				glBegin(GL_QUADS);
					norm = sqrt(pow(width*height/2,2)*pow(width*lenght/6,2));
					glNormal3f((width*height/2)/norm, (width*lenght/6)/norm, 0);
					glTexCoord2f(0.0f, 0.55f); glVertex3f(0.0, 0.0, 0.0);
					glTexCoord2f(1.0f, 0.55f); glVertex3f(0.0, 0.0, width);
					glTexCoord2f(1.0f, 0.635f); glVertex3f(lenght/6, -height/2, width);
					glTexCoord2f(0.0f, 0.635f); glVertex3f(lenght/6, -height/2, 0.0);

					norm = sqrt(pow(-width*height/2,2)*pow(width*lenght/6,2));
					glNormal3f((-width*height/2)/norm, (width*lenght/6)/norm, 0);
					glTexCoord2f(0.0f, 0.55f); glVertex3f(0.0, -height, 0.0);
					glTexCoord2f(0.0f, 0.635f); glVertex3f(lenght/6, -height/2, 0.0);
					glTexCoord2f(1.0f, 0.635f); glVertex3f(lenght/6, -height/2, width);
					glTexCoord2f(1.0f, 0.55f); glVertex3f(0.0, -height, width);
				glEnd();
				glBegin(GL_TRIANGLES);
					glNormal3f(0, 0, 1);
					glTexCoord2f(0.025f, 0.635f); glVertex3f(lenght/6, -height/2, width);
					glTexCoord2f(0.0f, 0.55f); glVertex3f(0.0, 0.0, width);
					glTexCoord2f(0.05f, 0.55f); glVertex3f(0.0, -height, width);

					glNormal3f(0, 0, -1);
					glTexCoord2f(0.025f, 0.635f); glVertex3f(lenght/6, -height/2, 0.0);
					glTexCoord2f(0.05f, 0.55f); glVertex3f(0.0, -height, 0.0);
					glTexCoord2f(0.0f, 0.55f); glVertex3f(0.0, 0.0, 0.0);
				glEnd();
			glPopMatrix();
			glPushMatrix();
				glTranslatef(-lenght/2, height/2, -width/2);
				glBegin(GL_QUADS);
					norm = sqrt(pow(width*height/2,2)*pow(-width*lenght/6,2));
					glNormal3f((width*height/2)/norm, (-width*lenght/6)/norm, 0);
					glTexCoord2f(0.0f, 0.55f); glVertex3f(0.0, 0.0, 0.0);
					glTexCoord2f(0.0f, 0.635f); glVertex3f(-lenght/6, -height/2, 0.0);
					glTexCoord2f(1.0f, 0.635f); glVertex3f(-lenght/6, -height/2, width);
					glTexCoord2f(1.0f, 0.55f); glVertex3f(0.0, 0.0, width);

					norm = sqrt(pow(-width*height/2,2)*pow(-width*lenght/6,2));
					glNormal3f((-width*height/2)/norm, (-width*lenght/6)/norm, 0);
					glTexCoord2f(0.0f, 0.55f); glVertex3f(0.0, -height, 0.0);
					glTexCoord2f(1.0f, 0.55f); glVertex3f(0.0, -height, width);
					glTexCoord2f(1.0f, 0.635f); glVertex3f(-lenght/6, -height/2, width);
					glTexCoord2f(0.0f, 0.635f); glVertex3f(-lenght/6, -height/2, 0.0);
				glEnd();
				glBegin(GL_TRIANGLES);
					glNormal3f(0, 0, 1);
					glTexCoord2f(0.025f, 0.635f); glVertex3f(-lenght/6, -height/2, width);
					glTexCoord2f(0.05f, 0.55f); glVertex3f(0.0, -height, width);
					glTexCoord2f(0.0f, 0.55f); glVertex3f(0.0, 0.0, width);

					glNormal3f(0, 0, -1);
					glTexCoord2f(0.0f, 0.55f); glVertex3f(0.0, 0.0, 0.0);
					glTexCoord2f(0.05f, 0.55f); glVertex3f(0.0, -height, 0.0);
					glTexCoord2f(0.025f, 0.635f); glVertex3f(-lenght/6, -height/2, 0.0);
				glEnd();
			glPopMatrix();
		glPopMatrix();
	glPopMatrix();
}

void Missiles::animate()
{
	oldX += sin(angle);
	oldZ += cos(angle);
}

void Missiles::reset()
{
	oldX = 0;
	oldZ = 0;
}

float Missiles::getHeight()
{
	return height;
}

Missiles::~Missiles(void)
{
	gluDeleteQuadric(quad);
}
